package util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@WebFilter(urlPatterns = { "/*" })
public class SessionCheckFilter implements Filter {
	private FilterConfig config;

	public SessionCheckFilter() {
	}

	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}

	public void destroy() {
		this.config = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpSession session = ((HttpServletRequest) request).getSession();
		String requestPath = ((HttpServletRequest) request).getServletPath();
		String func=(String)request.getParameter("func");
		if (session.getAttribute("loginUser") != null
				|| ("/message/manager".equals(requestPath) && func==null)
				|| "/user/login.jsp".equals(requestPath)
				|| "login".equals(func)
				|| "/user/register.jsp".equals(requestPath)
				|| "register".equals(func)
				|| "logout".equals(func)
				) {
			
			chain.doFilter(request, response);
		} else {
	
			//request.setAttribute("message", "请先登录");
			request.getRequestDispatcher("/user/login.jsp").forward(request, response);
		}

	}

}
